<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Request;

class Change extends Base
{
	public function anli_index()
	{
		$list = Db::table('anli')->select();
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function anli_edit()
	{
		$id = (int)input('get.id');
		$data = Db::table('anli')->where(array('id'=>$id))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function anli_update()
	{
		$id = (int)input('post.id');
        $data['name'] = trim(input('post.name'));
        $data['imga'] = trim(input('post.imga'));
        $data['imgc'] = trim(input('post.imgc'));
        $data['doctor'] = trim(input('post.doctor'));
        $data['imgb'] = trim(input('post.imgb'));
        $data['content'] = trim(input('post.content'));
        if($data['name'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'部位名称不能为空')));
        }
        if($data['doctor'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'案例医生不能为空')));
        }
        if($data['content'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'案例简介不能为空')));
        }
        $res=Db::table('anli')->where(array('id'=>$id))->update($data);
        if($res){
            exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
        }
	}
	// 图片上传
    public function upload_img(){
        $file = request()->file('file');
        if($file==null){
            exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
        }
        $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        $ext = ($info->getExtension());
        if(!in_array($ext,array('jpg','jpeg','gif','png'))){
            exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
        }
        $img = '/uploads/'.$info->getSaveName();
        exit(json_encode(array('code'=>0,'msg'=>$img)));
    }
    //手术日记
    public function diary_index()
    {
        $list = Db::table('diary')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加手术日记
    public function diary_add()
    {
        $id = (int)input('get.id');
        $data = Db::table('diary')->where(array('id'=>$id))->find();
        $atrr = Db::table('anli')->select();
        $this->assign('data',$data);
        $this->assign('atrr',$atrr);
        return $this->fetch();
    }
    public function diary_save()
    {
        $id = (int)input('post.id');
        $data['title'] = trim(input('post.title'));
        $data['pic'] = trim(input('post.pic'));
        $data['gid'] = (int)input('post.status');
        $data['content'] = trim(input('post.content'));

        if($data['title'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'手术人名不能为空')));
        }
        if($data['content'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'手术简介不能为空')));
        }
        if($id){
            $res = Db::table('diary')->where(array('id'=>$id))->update($data);
        }else{
            $res = Db::table('diary')->insert($data);
        }
        if(!$res){
            exit(json_encode(array('code'=>1,'msg'=>'保存失败')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
    //删除
    public function diary_delete(){
        $id = (int)input('post.id');
        $pic = Db::table('diary')->field('pic')->where(array('id'=>$id))->find();
        $urlpic=preg_replace("/\\\/","/",".".$pic['pic']);
        unlink($urlpic);
        $res = Db::table('diary')->where(array('id'=>$id))->delete();
        if(!$res){
            exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
    }
}
?>